<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Order extends Model
{
    //
    use SoftDeletes;

    protected $casts=[
        'channel_info'=>'json',
        'userinfo'=>'json',
        'confirm_loan'=>'json',
    ];

    protected $dates=['deleted_at'];

    protected $fillable=['channel_info','userinfo','status','reason','confirm_loan','mid','parent_mid'];

    public function getConfirmLoanAttribute($value)
    {
        $arr=json_decode($value,true);
//        dd($arr);
        if(!empty($arr)) {
//            $arr['agent_id']=isset($arr['agent_id'])?:'';
            $arr['fee_type'] = isset($arr['fee_type']) ? $arr['fee_type']  : '';
            $arr['fee_value'] = isset($arr['fee_value']) ? $arr['fee_value'] : '';
            if($arr['fee_type']){
                $arr['fee_value'] = $arr['loan_amount'] * $arr['fee_value'] / 100;
                $arr['fee_type'] = $arr['fee_type'] == 1 ? '按比例收费' : '单笔计费' ;
            }
//            $arr['product_id'] = isset($arr['product_id']) ?: '';
//            $arr['product_name'] = isset($arr['product_name']) ?: '';
//        $arr['loan_amount']=isset($arr['loan_amount'])?:'';
//        $arr['category_id_first']=isset($arr['category_id_first'])?:'';
//        $arr['category_id_second']=isset($arr['category_id_second'])?:'';
//        dd($arr['category_id_first']);
            /*if (array_key_exists('product_id', $arr)) {
                $productName = Product::find($arr['product_id']);
                if ($productName) {
                    $arr['product_name_json'] = $productName->name;
                }
            }*/
            if (array_key_exists('category_id_first', $arr)) {
                $Dictionary = Dictionary::find($arr['category_id_first']);
//            dd($Dictionary);
                if ($Dictionary) {
                    $arr['category_name_first'] = $Dictionary->name;
                }
            }
            if (array_key_exists('category_id_second', $arr)) {
                $Dictionary = Dictionary::find($arr['category_id_second']);
                if ($Dictionary) {
                    $arr['category_name_second'] = $Dictionary->name;
                }
            }
            if (array_key_exists('agent_id', $arr)) {

                $agent = Agent::find($arr['agent_id']);
                if ($agent) {
                    $arr['agent_name'] = $agent->name;
                }
            }
        }else{
            $arr = [];
        }
        return $arr;
    }

  /*  public function setConfirmLoanAttribute($confirmLoan)
    {
        $this->attributes['confirm_loan'] = json_encode($confirmLoan);
    }*/

    public function getUserinfoAttribute($userInfo)
    {
        $userInfo = json_decode($userInfo,true);
        $type =  Dictionary::find($userInfo['apply_type']);
        $userInfo['apply_type'] = $type ? $type->name : '';
        return $userInfo;
    }

    /**
     * @param $ids
     * @return array
     * 需要到处的订单数据
     */
    public function getExportData($all,$ids=null){
        if($all == 'all') {
            $cellDatas = Order::select('id','channel_info','userinfo','reason','confirm_loan','status','created_at')
                ->orderBy('id','desc')
                ->get();
        }else{
            $cellDatas = Order::select('id','channel_info','userinfo','reason','confirm_loan','status','created_at')
                ->orderBy('id','desc')
                ->whereIn('id',explode(',',$ids))
                ->get();
        }
        //表头
        $cellData[0] = ['订单ID','借款人','联系电话','身份证号','申请类别','申请金额(万元)','放款机构','放款产品','签单渠道','放款金额(万元)','签单人','申请时间','分销渠道','分销人','状态'];
        if(count($cellDatas)) {
            foreach ($cellDatas as $item) {
                if ($item['status'] == 0) {
                    $status = '申请中';
                } elseif ($item->status == 1) {
                    $status = '放款';
                } else {
                    $status = '退单';
                }
                $cellData[] = [$item['id'], $item->userinfo['customer'], $item->userinfo['phone'], $item->userinfo['idcard'], $item->userinfo['apply_type'], $item->userinfo['apply_amount'], $item->status == 1 ? $item->confirm_loan['agent_name'] : '', $item->status == 1 ? $item->confirm_loan['product_name'] : '', $item->channel['company_name'], $item->status == 1 ? $item->confirm_loan['loan_amount'] : '', $item->channel['manager'], $item->created_at, $item->channel['parent_company'], $item->channel['parent_manager'], $status];
            }
        }
        return $cellData;
    }
}
